/**
 * axiosInstance
 */
/**http://backend.5gzvip.91tunnel.com*/
import axios from "axios";
import router from "../router";
import { mainStore } from "../store/index";
import { ElNotification, ElMessage, ElMessageBox } from "element-plus";



const axiosInstance = axios.create({
    baseURL: baseURL,
    timeout: 25000
});


//请求拦截
axiosInstance.interceptors.request.use(config => {
    const store = mainStore();
    config.headers["token"] = store.token;
    return config;
})
axiosInstance.interceptors.response.use(resp => {
    // console.log('resp', resp)
    const store = mainStore();
    if (resp.data.code === "200") {
        if (resp.data.data === null) {
            return;
        } else {
            return Promise.resolve(resp.data.data);
        }
    } else if (resp.data.code === "10001") {
        // to re-login
        ElMessageBox.confirm('您已被注销，您可以取消停留在此页面，或重新登录', '确认登出', {
            confirmButtonText: '重新登录',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            router.replace('Login').then(() => {
                ElMessage({
                    type: 'success',
                    message: '欢迎登录'
                })
                location.reload()
            })
            // router.replace({
            //   path: '/login',
            // })
        });
    }
    else {
        console.log("请求正确，服务器错误");
        ElNotification({
            title: "系统提示",
            message: resp.data.message,
            type: "info",
            position: "top-left"
        });
        return Promise.reject(resp.data)
    }
}, error => {
    console.log("服务器错误", error);
    ElNotification({
        title: "系统提示",
        message: "系统错误,正在检查中···",
        type: "error",
        position: "top-left"
    });
    // if (error.response.data.code === 403) {
    //     router.replace({
    //         name: "Login"
    //     })
    // }
    return Promise.reject(error)
})
export default axiosInstance;
